package com.bgrconsulting.stockorderentry.service.rest.impl;


import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;


import com.bgrconsulting.stockorderentry.service.dao.AccountBalanceResponseDao;
import com.bgrconsulting.stockorderentry.service.dao.AccountResponseDao;
import com.bgrconsulting.stockorderentry.service.model.response.AccountBalanceResponse;

@Path("/accounts/rest/accountbalance/")
public class AccountBalanceResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String id;
	
	
	@GET
	 @Produces({"application/xml", "application/json"})
	 @Path("/{accountId}")  
	 public AccountBalanceResponse getAccount(@PathParam ("accountId") String accountId) {
		
		//TODO: Make this into an external call to a backend system.
		/*Account ac = new Account("INDIVUAL", accountId, "MARGIN", "", 657.37, "INDIVIDUAL");
		AccountBalance ab = new AccountBalance();
		ab.setCashAvailableForWithDrawal(0.00);
		ab.setNetAccountValue(-663.20);
		ab.setNetCash(740.450013);
		ab.setSweepDepositAmount(0.00);
		ab.setTotalLongValue(122.23);
		ab.setTotalSecurituesMktValue(-454.22);
		
		DTBalance dtb = new DTBalance();
		dtb.setDTCashBalance(0.00);
		dtb.setDTMarginBalance(0.00);
		dtb.setDTMarginSecurities(-4183.79);
		dtb.setDTNonMarginableSecuritiesAndOptions(-2714.88);
		dtb.setDTStatus("QIALIFIED_4_X");
		
		MarginAccountBalance mab = new MarginAccountBalance();
		
		mab.setDTBalance(dtb);
		mab.setFedCall(-105.49);
		mab.setMarginBalance(-740.45);
		mab.setMarginBalanceWithDrawal(-2714.88);
		mab.setMarginEquity(-714.88);
		mab.setMarginableSecurities(-4183.73);
		mab.setMaxAvailableForWithDrawal(-2714.88);
		mab.setMinEquityCall(-25663.20);
		mab.setNonMarginableSecuritiesAndOptions(-2714.88);
		
		
		CashAccountBalance cab = new CashAccountBalance();
		
		cab.setCashAvailableForInvestment(1000.23);
		cab.setCashBalance(203432.23);
		cab.setSettledCashForInvestment(12323.12);
		
		
		AccountBalanceResponse abr = new AccountBalanceResponse(ac, ab, mab, cab);
		
		AccountBalanceResponseConverter abrc = new AccountBalanceResponseConverter(abr);*/
		
		AccountBalanceResponse abrc = new AccountBalanceResponse();
		
		if(AccountBalanceResponseDao.instance.getModel().containsKey(accountId)) {
			abrc = AccountBalanceResponseDao.instance.getModel().get(accountId);
		}
		
		return abrc;
		
	
	}
	
	@PUT
	@Consumes({"application/xml","application/json"})
	public Response createNewAccountBalance (JAXBElement <AccountBalanceResponse> accountBalanceResponse) {
		
		
		return putAndGetResponse(accountBalanceResponse.getValue());
		
    }
	
	
	
	private Response putAndGetResponse(AccountBalanceResponse accountBalanceResponse) {
		Response res;
		if(AccountResponseDao.instance.getModel().containsKey(accountBalanceResponse.getAccount().getAccountId())) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		AccountBalanceResponseDao.instance.getModel().put(accountBalanceResponse.getAccount().getAccountId(), accountBalanceResponse);
		return res;
	}
}
